/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package privileges.bean;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import privileges.business.UserBusiness;

/**
 *
 * @author Javier
 */
@ManagedBean
@RequestScoped
public class EditGlobalPrivilegesBean implements Serializable{
    
    @ManagedProperty(value="#{advancedPrivilegesBean.nameUser}")
    private String user;
    @ManagedProperty(value="#{advancedPrivilegesBean.host}")
    private String host;
    private String userPrivileges;
    private String maxQueriesPerHour;
    private String maxUpdatesPerHour;
    private String maxConnectionsPerHour;
    private String maxUserConnections;
    private boolean checkData;
    private boolean checkStructure;
    private boolean checkAdmin;
    private List<String> selectedDataOptions;
    private List<String> selectedAdminOptions;
    private List<String> selectedStructOptions;
    //private AdvancedPrivilegesBean apb ;
            

    public EditGlobalPrivilegesBean() {
     /*   apb = (AdvancedPrivilegesBean) FacesContext.getCurrentInstance()
                  .getExternalContext().getSessionMap().get("advancedPrivilegesBean");
        this.user = apb.getNameUser();
        this.host = apb.getHost();
        this.userPrivileges = getPrivileges();
        reloadTables();
        reloadGrantOption();*/
        this.checkData = false;
        this.checkAdmin = false;
        this.checkStructure = false;
    }
    
    @PostConstruct
    public void init(){
        this.userPrivileges = getPrivileges();
        reloadTables();
        reloadGrantOption();
    }

    public boolean isCheckData() {
        return checkData;
    }

    public void setCheckData(boolean checkData) {
        this.checkData = checkData;
    }

    public boolean isCheckStructure() {
        return checkStructure;
    }

    public void setCheckStructure(boolean checkStructure) {
        this.checkStructure = checkStructure;
    }

    public boolean isCheckAdmin() {
        return checkAdmin;
    }

    public void setCheckAdmin(boolean checkAdmin) {
        this.checkAdmin = checkAdmin;
    }
    
    private void reloadGrantOption() {
        FacesContext context = FacesContext.getCurrentInstance();
        List<String> maxGO = new ArrayList<>();
        try {
            maxGO = new UserBusiness().getMaxGO(user, host);
            maxConnectionsPerHour = maxGO.get(0);
            maxQueriesPerHour = maxGO.get(1);
            maxUpdatesPerHour = maxGO.get(2);
            maxUserConnections = maxGO.get(3);
        } catch (SQLException ex) {
            ex.printStackTrace();
            context.addMessage(null, new FacesMessage("Error", ex.getLocalizedMessage()));
        }

    }
    private void reloadTables(){
        selectedDataOptions = getSelectedDataOptionsLoad();
        selectedAdminOptions = getSelectedAdminOptionsLoad();
        selectedStructOptions = getSelectedStructOptionsLoad();
        System.out.println("data "+selectedDataOptions.toString());
        System.out.println("admin "+ selectedAdminOptions.toString());
        System.out.println("struc " + selectedStructOptions.toString());
    }

    public String getMaxQueriesPerHour() {
        return maxQueriesPerHour;
    }

    public void setMaxQueriesPerHour(String maxQueriesPerHour) {
        this.maxQueriesPerHour = maxQueriesPerHour;
    }

    public String getMaxUpdatesPerHour() {
        return maxUpdatesPerHour;
    }

    public void setMaxUpdatesPerHour(String maxUpdatesPerHour) {
        this.maxUpdatesPerHour = maxUpdatesPerHour;
    }

    public String getMaxConnectionsPerHour() {
        return maxConnectionsPerHour;
    }

    public void setMaxConnectionsPerHour(String maxConnectionsPerHour) {
        this.maxConnectionsPerHour = maxConnectionsPerHour;
    }

    public String getMaxUserConnections() {
        return maxUserConnections;
    }

    public void setMaxUserConnections(String maxUserConnections) {
        this.maxUserConnections = maxUserConnections;
    }
    
    private String getPrivileges(){
        try {
            return new UserBusiness().getUserGlobalPrivileges(user,host);
        } catch (SQLException ex) { 
            return "";
        }
    }
    private List<String> getSelectedDataOptionsLoad() {
        return new UserBusiness().getDaOp(userPrivileges);
    }

    private List<String> getSelectedAdminOptionsLoad() {
        return new UserBusiness().getAdOp(userPrivileges);
    }

    private List<String> getSelectedStructOptionsLoad() {
        return new UserBusiness().getSeStruOp(userPrivileges);
    }

    public String getUserPrivileges() {
        return userPrivileges;
    }

    public void setUserPrivileges(String userPrivileges) {
        this.userPrivileges = userPrivileges;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public List<String> getSelectedDataOptions() {
        return selectedDataOptions;
    }

    public void setSelectedDataOptions(List<String> selectedDataOptions) {
        this.selectedDataOptions = selectedDataOptions;
    }

    public List<String> getSelectedAdminOptions() {
        return selectedAdminOptions;
    }

    public void setSelectedAdminOptions(List<String> selectedAdminOptions) {
        this.selectedAdminOptions = selectedAdminOptions;
    }

    public List<String> getSelectedStructOptions() {
        return selectedStructOptions;
    }

    public void setSelectedStructOptions(List<String> selectedStructOptions) {
        this.selectedStructOptions = selectedStructOptions;
    } 
    public void changeCheckData(){
        if(checkData){
            List<String> selectedDataOptionsTemp = new ArrayList<>();
            selectedDataOptionsTemp.add("SELECT");
            selectedDataOptionsTemp.add("INSERT");
            selectedDataOptionsTemp.add("UPDATE");
            selectedDataOptionsTemp.add("DELETE");
            selectedDataOptionsTemp.add("FILE");
            selectedDataOptions = selectedDataOptionsTemp;
        }else{
            selectedDataOptions = new ArrayList<>();
        }
    }
        
    public void changeCheckAdmin(){
        if(checkAdmin){
            List<String> selectedAdminOptionsTemp = new ArrayList<>();
            selectedAdminOptionsTemp.add("GRANT OPTION");
            selectedAdminOptionsTemp.add("SUPER");
            selectedAdminOptionsTemp.add("PROCESS");
            selectedAdminOptionsTemp.add("RELOAD");
            selectedAdminOptionsTemp.add("SHUTDOWN");
            selectedAdminOptionsTemp.add("SHOW DATABASES");
            selectedAdminOptionsTemp.add("LOCK TABLES");
            selectedAdminOptionsTemp.add("REFERENCES");
            selectedAdminOptionsTemp.add("REPLICATION CLIENT");
            selectedAdminOptionsTemp.add("REPLICATION SLAVE");
            selectedAdminOptionsTemp.add("CREATE USER");
            selectedAdminOptions = selectedAdminOptionsTemp;
        }else{
            selectedAdminOptions = new ArrayList<>();
        }
    }
    
   public void changeCheckStructure(){
        if(checkStructure){
            List<String> selectedStructureOptionsTemp = new ArrayList<>();
            selectedStructureOptionsTemp.add("CREATE");
            selectedStructureOptionsTemp.add("ALTER");
            selectedStructureOptionsTemp.add("INDEX");
            selectedStructureOptionsTemp.add("DROP");
            selectedStructureOptionsTemp.add("CREATE TEMPORARY TABLES");
            selectedStructureOptionsTemp.add("SHOW VIEW");
            selectedStructureOptionsTemp.add("CREATE ROUTINE");
            selectedStructureOptionsTemp.add("ALTER ROUTINE");
            selectedStructureOptionsTemp.add("EXECUTE");
            selectedStructureOptionsTemp.add("CREATE VIEW");
            selectedStructureOptionsTemp.add("EVENT");
            selectedStructureOptionsTemp.add("TRIGGER");
            selectedStructOptions = selectedStructureOptionsTemp;
        }else{
            selectedStructOptions = new ArrayList<>();
        }
    }
   private boolean isSomethingCheck(){
        return (!selectedAdminOptions.isEmpty())
                || (!selectedDataOptions.isEmpty())
                || (!selectedStructOptions.isEmpty()); 
    }
    public void changePrivileges() {
        System.out.println("edit globla" + user + host);
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            if (maxConnectionsPerHour.equals("")
                    || maxQueriesPerHour.equals("")
                    || maxUpdatesPerHour.equals("")
                    || maxUserConnections.equals("")) {
                context.addMessage(null, new FacesMessage("Error", "Uno o mas espacios estan en blanco"));
            } else {
                if (isSomethingCheck()) {
                    new UserBusiness().doGlobalPrivileges(user, host, selectedAdminOptions, selectedDataOptions, selectedStructOptions, maxConnectionsPerHour, maxQueriesPerHour, maxUpdatesPerHour, maxUserConnections);
                    context.addMessage(null, new FacesMessage("Exito", "Privilegios Agregados Exitosamente"));
                } else {
                    context.addMessage(null, new FacesMessage("Error", "Debe Seleccionar por lo menos un privilegio"));

                }

            }

        } catch (SQLException ex) {
            context.addMessage(null, new FacesMessage("Error", ex.getLocalizedMessage()));
        }
    }
}
